<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
$msg            = "Record Not Found";
$partyArray     = array();
$itemArray      = array();
$sizeArray      = array();
$companyArray   = array();

if(isset($_POST['submit']))
{
	$partyId   = $_POST['partyId'];
	$orderDate = $_POST['billDateYear']."-".$_POST['billDateMonth']."-".$_POST['billDateDay'];
  $insertOrder = "INSERT INTO order (orderDate,partyId)
                        VALUE (".$orderDate.",'".$partyId."')";
  $insertOrderRes = mysql_query($insertOrder);
  $orderId = mysql_insert_id();
  if(!$insertOrderRes)
  {
    die("Insert Query Not Inserted : ".mysql_error());
  }
  else
  {
    $loopCntDetail = 0;
    	$itemId    = isset($_POST['itemId'][$loopCntDetail]) ? $_POST['itemId'][$loopCntDetail] : 0;
    	$sizeId    = isset($_POST['sizeId'][$loopCntDetail]) ? $_POST['sizeId'][$loopCntDetail] : 0;
    	$companyId = isset($_POST['companyId'][$loopCntDetail]) ? $_POST['companyId'][$loopCntDetail] : 0;
    	$qualityId = isset($_POST['qualityId'][$loopCntDetail]) ? $_POST['qualityId'][$loopCntDetail] : 0;
    	$colorId   = isset($_POST['colorId'][$loopCntDetail]) ? $_POST['colorId'][$loopCntDetail] : 0;
    	$rate      = isset($_POST['rate'][$loopCntDetail]) ? $_POST['rate'][$loopCntDetail] : 0;
    	$amount    = isset($_POST['amount'][$loopCntDetail]) ? $_POST['amount'][$loopCntDetail] : 0;
    
      if($companyId > 0)
      {
	      $insertPartyDetail = "INSERT INTO orderdetail (orderId,itemId,sizeId,companyId,qualityId,colorId,rate,amount)
	                                         VALUES(".$orderId.",".$itemId.",".$sizeId.",".$companyId.",".$qualityId.",
	                                                ".$colorId.",".$rate.",".$amount.")";
	      $insertPartyDetailRes = mysql_query($insertPartyDetail);
	      if(!$insertPartyDetailRes)
	      {
	      	echo "data not Inserted ".mysql_error();
	      }
      }
       //header("location:toolPurchaseVr.php");
  }
}
$i = 0;
$selectParty = "SELECT partyId,partyName
                  FROM party
                 ORDER BY partyName";
$selectPartyResult = mysql_query($selectParty);
while($partyRow = mysql_fetch_array($selectPartyResult))
{
	$partyArray['partyId'][$i]     = $partyRow['partyId'];
	$partyArray['partyName'][$i]   = $partyRow['partyName'];
	$i++;
}

$k = 0;
$selectItem = "SELECT itemId,itemName
                 FROM item 
                ORDER BY itemName";
$selectItemResult = mysql_query($selectItem);
while($itemRow = mysql_fetch_array($selectItemResult))
{
	$itemArray['itemId'][$k]     = $itemRow['itemId'];
	$itemArray['itemName'][$k]   = $itemRow['itemName'];
	$k++;
}


$m = 0;
$selectItem = "SELECT companyId,companyName
                 FROM company 
                ORDER BY companyName";
$selectItemResult = mysql_query($selectItem);
while($itemRow = mysql_fetch_array($selectItemResult))
{
	$companyArray['companyId'][$m]     = $itemRow['companyId'];
	$companyArray['companyName'][$m]   = $itemRow['companyName'];
	$m++;
}

$smarty->assign('arrayOrder', array(1 => 'Order Aapvaa', 2 => 'Order Levaa', 3 => 'Bill Incoming', 4 => 'Bill Outgoing'));
include("./bottom.php");
$smarty->assign("partyArray",$partyArray);
$smarty->assign("itemArray",$itemArray);
$smarty->assign("companyArray",$companyArray);
$smarty->assign("msg",$msg);
$smarty->display('order.tpl');
?>